package cn.jinjimi.app.main.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import cn.jinjimi.app.R;

import java.io.InputStream;

/**
 * 验证码校验框
 * <p>
 * Created by linlongjie on 2017/11/6.
 */
public class VerifyCodeDialog extends Dialog implements View.OnClickListener {
    private Context mContext = null;

    private ImageView mVerifyImage = null;
    private EditText mVerifyCode = null;
    private TextView mRightBtn = null;
    private TextView mLeftBtn = null;
    private ProgressBar mProgressBar;

    private Bitmap mBitmap = null;

    public VerifyCodeDialog(Context context) {
        this(context, R.style.verify_dialog_style);
    }

    public VerifyCodeDialog(Context context, int theme) {
        super(context, theme);
        mContext = context;
        setContentView(R.layout.dialog_verify_code_layout);

        mVerifyImage = (ImageView) findViewById(R.id.iv_verify);
        mVerifyCode = (EditText) findViewById(R.id.et_verify);
        mRightBtn = (TextView) findViewById(R.id.tv_alert_ok);
        mRightBtn.setVisibility(View.VISIBLE);
        mRightBtn.setOnClickListener(this);

        mLeftBtn = (TextView) findViewById(R.id.tv_alert_cancel);
        mLeftBtn.setVisibility(View.GONE);
        mLeftBtn.setOnClickListener(this);

        mProgressBar = (ProgressBar) findViewById(R.id.progress_bar);

        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    }

    /**
     * 设置dialog的校验图
     *
     * @param imgStream
     */
    public VerifyCodeDialog setVerifyImg(InputStream imgStream) {
        mBitmap = BitmapFactory.decodeStream(imgStream); //转为文件
        mVerifyImage.setImageBitmap(mBitmap);
        return this;
    }

    public VerifyCodeDialog setTopListener(View.OnClickListener listener) {
        mVerifyImage.setVisibility(View.VISIBLE);
        if (listener != null)
            mVerifyImage.setOnClickListener(listener);
        return this;
    }

    /**
     * 设置取消按钮和事件处理
     *
     * @param listener
     */
    public VerifyCodeDialog setLeftListener(View.OnClickListener listener) {
        mLeftBtn.setVisibility(View.VISIBLE);
        if (listener != null)
            mLeftBtn.setOnClickListener(listener);
        return this;
    }

    /**
     * 设置确认按钮和事件处理
     *
     * @param listener
     */
    public VerifyCodeDialog setRightListener(View.OnClickListener listener) {
        mRightBtn.setVisibility(View.VISIBLE);
        if (listener != null)
            mRightBtn.setOnClickListener(listener);
        return this;
    }

    public void setLoading(boolean isLoading) {
        mProgressBar.setVisibility(isLoading ? View.VISIBLE : View.GONE);
    }


    public boolean showDialog(boolean checkAtyIsFinish) {
        if (checkAtyIsFinish && ((Activity) mContext).isFinishing()) {
            return false;
        }
        if (isShowing()) {
            return false;
        }
        show();
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_alert_ok:
                dismissDialog();
                break;
            case R.id.tv_alert_cancel:
                dismissDialog();
                break;
        }
    }

    public String getVerifyCode() {
        return mVerifyCode == null || mVerifyCode.getText() == null ? "" : mVerifyCode.getText().toString();
    }

    public void destory() {
        mContext = null;
    }

    public boolean dismissDialog() {
        if (isShowing()) {
            try {
                if (mBitmap != null) {
                    mBitmap.recycle();
                    mBitmap = null;
                }
                dismiss();
            } catch (Exception e) {
            }
            return true;
        }
        return false;
    }

    public void clear() {
        if (mVerifyCode != null)
            mVerifyCode.setText("");
    }
}
